package com.foolishbird.sdk.ui.activity;

import android.Manifest;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.widget.Toast;


import androidx.fragment.app.DialogFragment;
import androidx.fragment.app.Fragment;

import com.foolishbird.sdk.assist.CallbackObservable;
import com.foolishbird.sdk.assist.Constant;
import com.foolishbird.sdk.core.HttpEngineApi;
import com.foolishbird.sdk.core.HttpEngineConduits;
import com.foolishbird.sdk.handler.SimpleAsyncResponseHandler;
import com.foolishbird.sdk.params.BindPhoneResponseParams;
import com.foolishbird.sdk.params.LoginResponseParams;
import com.foolishbird.sdk.params.ModifyPasswordResponseParams;
import com.foolishbird.sdk.params.ResponseParams;
import com.foolishbird.sdk.params.bean.AccountInfo;
import com.foolishbird.sdk.params.bean.GameNoticeModel;
import com.foolishbird.sdk.params.bean.Goods;
import com.foolishbird.sdk.params.bean.LoginInfo;
import com.foolishbird.sdk.params.bean.Popup;
import com.foolishbird.sdk.params.bean.PopupConfig;
import com.foolishbird.sdk.params.bean.State;
import com.foolishbird.sdk.ui.assist.LoginTypeSwitchWatcher;
import com.foolishbird.sdk.ui.content.TimeTaskService;
import com.foolishbird.sdk.ui.fragment.BaseDialogFragment;
import com.foolishbird.sdk.ui.fragment.CertifiesTipDialogFragment;
import com.foolishbird.sdk.ui.fragment.FoolishBirdBindDialogFragment;
import com.foolishbird.sdk.ui.fragment.FoolishBirdChangePwdDialogFragment;
import com.foolishbird.sdk.ui.fragment.FoolishBirdLoginAutoFragment;
import com.foolishbird.sdk.ui.fragment.FoolishBirdLoginFragment;
import com.foolishbird.sdk.ui.fragment.FoolishBirdLoginRegisterFragment;
import com.foolishbird.sdk.ui.fragment.FoolishBirdNoticeFragment;
import com.foolishbird.sdk.ui.fragment.GuestRegisterDialogFragment;
import com.foolishbird.sdk.ui.fragment.NoticeTipDialogFragment;
import com.foolishbird.sdk.utils.WL;
import com.foolishbird.sdk.utils.XmlUtils;
import com.tencent.mm.opensdk.utils.Log;

import java.util.ArrayList;
import java.util.List;
import java.util.Stack;


public class UserLoginActivity extends PhoneVerifyActivity implements FoolishBirdLoginFragment.OnUserActionListener,
        FoolishBirdChangePwdDialogFragment.OnChangeUserPasswordListener, FoolishBirdLoginRegisterFragment.OnQuickRegisterListener,
        FoolishBirdBindDialogFragment.OnRealNameVerifyListener,
        GuestRegisterDialogFragment.OnGuestActionListener, FoolishBirdLoginAutoFragment.OnAutoLoginActionListener {

    private static final String TAG = UserLoginActivity.class.getSimpleName();

    private static final String FLAG_LOGIN_FRAGMENT = "login";
    private static final String FLAG_AUTO_LOGIN_FRAGMENT = "auto_login";
    private static final String FLAG_GUEST_REGISTER_FRAGMENT = "guest";
    private static final String FLAG_PASSWORD_FRAGMENT = "password";
    private static final String FLAG_REGISTER_FRAGMENT = "register";
    private static final String FLAG_REALNAME_FRAGMENT = "realname";
    private static final String FLAG_NOTICE_FRAGMENT = "notice";
    private static final String FLAG_ACCOUNT_FRAGMENT = "account";
    private static final String FLAG_ACCOUNT_LIST_FRAGMENT = "account_list";
    private static final String FLAG_CERTIFIES_FRAGMENT = "certifies";
    private static final String FLAG_VERSION_UPDATE_FRAGMENT = "version_update";
    private static final String FLAG_GAME_NOTICE_FRAGMENT = "game_notice";

    private String Bundle_Login = "Bundle_login";
    private String Bundle_Notice = "notice";
    private String Bundle_IsBindPhone = "Bundle_isBindPhone";


    private int mSubmitLoginInfoTaskId;
    private int mSumbitUserPasswordInfoTaskId;
//    private int mSumbitRegisterInfoTaskId;
    private int mSumbitVerifyInfoTaskId;

    private FoolishBirdLoginFragment mLoginFragment;
    private GuestRegisterDialogFragment mGuestRegisterFragment;
    private FoolishBirdLoginAutoFragment mAutoLoginDialogFragment;
//    private DownloadDialogFragment mVersionUpdateDialogFragment;
    private FoolishBirdChangePwdDialogFragment mChangePasswordFragment;
    private FoolishBirdLoginRegisterFragment mQuickRegisterFragment;
    private FoolishBirdBindDialogFragment mRealNameVerifyFragment;
    private NoticeTipDialogFragment mNoticeTipFragment;

    private CertifiesTipDialogFragment mCertifiesTipFragment;
    private FoolishBirdNoticeFragment mGameNoticeFragment;

    private int mCurrentLoginType = LoginTypeSwitchWatcher.SWITCH_MODE_WHICH_ACCOUNT;

    public final void setCurrentLoginType(int loginType) {
        this.mCurrentLoginType = loginType;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        checkAppSelfPermission(Manifest.permission.READ_PHONE_STATE);

        //是否为支付前实名认证
        if (isRealNameVerifyBeforePayment()) {
//            PopupConfig popupConfig = new PopupConfig();
//            popupConfig.setNoticePopup(2);
//            popupConfig.setPhonePopup(2);
//            popupConfig.setIdcardPopup(1);
//            popupConfig.setIdcardPopupCloseEnable(1);

            Bundle bundle = new Bundle();
            bundle.putParcelable("goods", getIntent().getParcelableExtra("goods"));
//            bundle.putParcelable("PopupConfig", popupConfig);
            bundle.putBoolean(Bundle_IsBindPhone,false);
            doNextToCertifiesTip(null, bundle);
        }

        //是否为游客登录定时提示实名认证
        else if (isOnlyRealNameVerify()) {
            PopupConfig popupConfig = getIntent().getParcelableExtra(Constant.LocalIntentExtra.INTENT_KEY_LOGIN_BINDING_DATA);
            Bundle bundle = new Bundle();
//            bundle.putParcelable("PopupConfig", popupConfig);
            bundle.putBoolean(Bundle_IsBindPhone,true);
//            df
            doNextToRealNameVerify(null, bundle);
//            donextto
        }

        //检查是否有新版本更新升级
//        else if (CommonUtils.isUpdateVersion()) {
//            if (CommonUtils.isForceUpdateVersion() || CommonUtils.existsDownloadedVersion()) {
//                doNextToVersionUpdate(null);
//            } else {
//                HttpEngineConduits conduits = HttpEngineApi.getInstance().getEngineConduits();
//                VersionInfo newVersionInfo = conduits.readVersionInfo();
//
//                //启动版本静默下载
//                VersionUpdateDownloader versionUpdateManager = VersionUpdateDownloader.getImpl();
//                File storageFile = versionUpdateManager.randomMakeDownloadStorageFile(String.valueOf(newVersionInfo.getVersionCode()));
//                versionUpdateManager.startVersionDownloadTask(newVersionInfo.getUrl(), storageFile, false);
//
//                //执行登录
//                executeLoginProcess(null);
//            }
//        }

        //执行登录
        else {
            executeLoginProcess(null);
        }

    }

    private void executeLoginProcess(DialogFragment fragment) {
        HttpEngineConduits wanSDKDataDao = HttpEngineApi.getInstance().getEngineConduits();
        List<AccountInfo> accountInfos = wanSDKDataDao.readAllUserAccountInfo();
        if (XmlUtils.isPutInChannel() && (accountInfos == null || accountInfos.isEmpty())) {
            doNextToGuestRegister(fragment);
        } else {
            if (!wanSDKDataDao.readAutoLogin()||accountInfos == null || accountInfos.isEmpty()) {
                doNextToLogin(fragment, wanSDKDataDao.readLastLoginType());
            }else {
                doNextToAutoLogin(fragment);
            }

        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        HttpEngineApi.getInstance().cancel(mSubmitLoginInfoTaskId);
        HttpEngineApi.getInstance().cancel(mSumbitUserPasswordInfoTaskId);
//        HttpEngineApi.getInstance().cancel(mSumbitRegisterInfoTaskId);
        HttpEngineApi.getInstance().cancel(mSumbitVerifyInfoTaskId);
    }

    private boolean isOnlyRealNameVerify() {
        Intent intent = getIntent();
        if (intent != null) {
            String loginInitPosition = intent.getStringExtra(Constant.LocalIntentExtra.INTENT_KEY_LOGIN_INIT_POSITION);
            return TextUtils.equals(Constant.LocalIntentExtra.INTENT_VALUE_LOGIN_INIT_BINDING, loginInitPosition);
        }
        return false;
    }

    private boolean isRealNameVerifyBeforePayment() {
        Intent intent = getIntent();
        if (intent != null) {
            return intent.hasExtra("goods");
        }
        return false;
    }

    private void switchDialogFragment(DialogFragment from, DialogFragment to, String tag) {
        if (from != null && !isSaveInstanceState()) {
            from.dismissAllowingStateLoss();
        }

        if (to != null && !isSaveInstanceState()) {
            to.show(getSupportFragmentManager(), tag);
        }
    }


    public void doSubmitLoginAccountInfo(String username, String verify) {
        SimpleAsyncResponseHandler handler = requestCallbackHandler(LoginResponseParams.class, username, verify, "false");
        mSubmitLoginInfoTaskId = HttpEngineApi.getInstance().loginByVerify(username, verify, handler);
    }

    @Override
    public void doSubmitLoginInfo(String username, String password) {
        SimpleAsyncResponseHandler handler = requestCallbackHandler(LoginResponseParams.class, username, password, "false");
        mSubmitLoginInfoTaskId = HttpEngineApi.getInstance().login(username, password, handler);
    }

    @Override
    public void doAutoLoginInfo(String username, String password) {
        SimpleAsyncResponseHandler handler = requestCallbackHandler(LoginResponseParams.class, username, password, "true");
        mSubmitLoginInfoTaskId = HttpEngineApi.getInstance().login(username, password, true, handler);
    }

    @Override
    public void doSubmitCellPhoneNumber(String cellphone, String verify) {
//        SimpleAsyncResponseHandler handler = requestCallbackHandler(AccountInfosResponseParams.class, cellphone, verify);
//        mSubmitLoginInfoTaskId = HttpEngineApi.getInstance().getAccountInfosByPhone(cellphone, verify, handler);
//        dfdf
        doSubmitLoginAccountInfo(cellphone, (String) verify);
    }

    @Override
    public void doNextToOneKeyLogin() {
//        SimpleAsyncResponseHandler handler = requestCallbackHandler(QuickRegisterResponseParams.class);
        SimpleAsyncResponseHandler handler = requestCallbackHandler(LoginResponseParams.class);
        mSubmitLoginInfoTaskId = HttpEngineApi.getInstance().quickRegister(handler);
    }

    @Override
    public void doNextToQuickRegister() {
        mQuickRegisterFragment = FoolishBirdLoginRegisterFragment.newInstance();
        if (mGuestRegisterFragment != null && mGuestRegisterFragment.isResumed()) {
            switchDialogFragment(mGuestRegisterFragment, mQuickRegisterFragment, FLAG_REGISTER_FRAGMENT);
        } else {
            switchDialogFragment(mLoginFragment, mQuickRegisterFragment, FLAG_REGISTER_FRAGMENT);
        }
    }

    @Override
    public void doNextToChangePassword(String username) {
        Bundle bundle = new Bundle();
        bundle.putString("username", username);

        mChangePasswordFragment = FoolishBirdChangePwdDialogFragment.newInstance(bundle);
        setOnPhoneVerifyCodeListener(mChangePasswordFragment);
        switchDialogFragment(mLoginFragment, mChangePasswordFragment, FLAG_PASSWORD_FRAGMENT);
    }

    private void doNextToRealNameVerify(DialogFragment fragment, Bundle bundle) {
        mRealNameVerifyFragment = FoolishBirdBindDialogFragment.newInstance(bundle);
        switchDialogFragment(fragment, mRealNameVerifyFragment, FLAG_REALNAME_FRAGMENT);
    }

//    private void doNextToOneKeyLogin(Bundle bundle) {
//        mAccountFragment = AccountDialogFragment.newInstance(bundle);
//        switchDialogFragment(mLoginFragment, mAccountFragment, FLAG_ACCOUNT_FRAGMENT);
//    }

    private void doNextToNoticeTip(DialogFragment fragment, Bundle bundle) {
        mNoticeTipFragment = NoticeTipDialogFragment.newInstance(bundle);
        switchDialogFragment(fragment, mNoticeTipFragment, FLAG_NOTICE_FRAGMENT);
    }

//    private void doNextToAccountList(DialogFragment fragment, Bundle bundle) {
//        mAccountListDialogFragment = AccountListDialogFragment.newInstance(bundle);
//        switchDialogFragment(fragment, mAccountListDialogFragment, FLAG_ACCOUNT_LIST_FRAGMENT);
//    }

    private void doNextToCertifiesTip(DialogFragment fragment, Bundle bundle) {
        mCertifiesTipFragment = CertifiesTipDialogFragment.newInstance(bundle);
        switchDialogFragment(fragment, mCertifiesTipFragment, FLAG_CERTIFIES_FRAGMENT);
    }

    private void doNextToLogin(DialogFragment fragment, int currentLoginType) {
        this.mCurrentLoginType = currentLoginType;
        mLoginFragment = FoolishBirdLoginFragment.newInstance(currentLoginType);
        switchDialogFragment(fragment, mLoginFragment, FLAG_LOGIN_FRAGMENT);
    }

    private void doNextToGameNotice(DialogFragment fragment,Bundle bundle){
        mGameNoticeFragment = FoolishBirdNoticeFragment.newInstance(bundle);
        switchDialogFragment(fragment,mGameNoticeFragment,FLAG_GAME_NOTICE_FRAGMENT);
    }

    private void doNextToGuestRegister(DialogFragment fragment) {
        mGuestRegisterFragment = GuestRegisterDialogFragment.newInstance();
        switchDialogFragment(fragment, mGuestRegisterFragment, FLAG_GUEST_REGISTER_FRAGMENT);
    }

    private void doNextToAutoLogin(DialogFragment fragment) {
        mAutoLoginDialogFragment = FoolishBirdLoginAutoFragment.newInstance();
        switchDialogFragment(fragment, mAutoLoginDialogFragment, FLAG_AUTO_LOGIN_FRAGMENT);
    }

//    private void doNextToVersionUpdate(DialogFragment fragment) {
//        mVersionUpdateDialogFragment = DownloadDialogFragment.newInstance();
//        switchDialogFragment(fragment, mVersionUpdateDialogFragment, FLAG_VERSION_UPDATE_FRAGMENT);
//    }

    @Override
    public void doFinishActivity(Fragment fragment) {
        if (fragment instanceof FoolishBirdLoginAutoFragment) {
            doNextToLogin(mAutoLoginDialogFragment, LoginTypeSwitchWatcher.SWITCH_MODE_WHICH_ACCOUNT);
//        } else if (fragment instanceof DownloadDialogFragment) {
//            Bundle bundle = fragment.getArguments();
//            if (bundle.getBoolean("executeLogin")) {
//                executeLoginProcess(mVersionUpdateDialogFragment);
//            } else {
//                finish();
//            }
        } else if (fragment instanceof GuestRegisterDialogFragment) {
            HttpEngineConduits wanSDKDataDao = HttpEngineApi.getInstance().getEngineConduits();
            doNextToLogin(mGuestRegisterFragment, wanSDKDataDao.readLastLoginType());
        } else if (fragment instanceof FoolishBirdLoginFragment) {
            String message = getString(getStringIdentifier("hnssn_sdk_messag_login_cancel"));
            doCallbackLoginResult(Constant.LoginState.LOGIN_CANCEL, message, null);
//        } else if (fragment instanceof AccountDialogFragment) {
//            Bundle fragmentArguments = fragment.getArguments();
//            if (fragmentArguments != null) {
//                QuickRegisterResponseParams.QuickRegisterResponseData data = fragmentArguments.getParcelable("quickRegister");
//
//                WL.w(TAG, "login finished.");
//                //跳转到绑定手机号码页面
//                LoginInfo loginInfo = new LoginInfo(data.getId(), data.getToken(), data.getExpires_in());
//                judgeLoginNextOperationSteps(data.getPopupConfig(),loginInfo,(AccountDialogFragment)fragment);
//
//
////                String message = getString(getStringIdentifier("hnssn_sdk_messag_login_successs"));
////                doCallbackLoginResult(Constant.LoginState.LOGIN_SUCCESS, message, loginInfo);
//                //启动定时器服务，提示用户绑定手机/实名认证
//                TimeTaskService.enqueueWork(this, data.getPopupConfigs());
//
//
//            }
        } else if (fragment instanceof FoolishBirdChangePwdDialogFragment) {
            doNextToLogin(mChangePasswordFragment, mCurrentLoginType);
        } else if (fragment instanceof FoolishBirdLoginRegisterFragment) {
            doNextToLogin(mQuickRegisterFragment, mCurrentLoginType);
//        } else if (fragment instanceof AccountListDialogFragment) {
//            doNextToLogin(mAccountListDialogFragment, mCurrentLoginType);
        } else if (fragment instanceof NoticeTipDialogFragment) {
            Bundle bundle = fragment.getArguments();
            LoginInfo loginInfo = bundle.getParcelable("loginInfo");
            String message = getString(getStringIdentifier("hnssn_sdk_messag_login_successs"));
            doCallbackLoginResult(Constant.LoginState.LOGIN_SUCCESS, message, loginInfo);
        } else if (fragment instanceof FoolishBirdBindDialogFragment) {
            judgeBindingNextOperationSteps((BaseDialogFragment)fragment, true);
        } else if (fragment instanceof CertifiesTipDialogFragment) {
            doNextToRealNameVerify(mCertifiesTipFragment, fragment.getArguments());
        } else if (fragment instanceof FoolishBirdNoticeFragment){
            Bundle bundle = fragment.getArguments();
            LoginResponseParams.LoginResponseData data =(LoginResponseParams.LoginResponseData)bundle.getSerializable(Bundle_Login);
//            LoginInfo loginInfo = bundle.getParcelable("loginInfo");
//            PopupConfig popupConfig = bundle.getParcelable("popupConfig");
//            popupConfig.setGameNoticeList(null);
            judgeLoginNextOperationSteps(data,(FoolishBirdNoticeFragment)fragment);
        }
        else{
            finish();
        }
    }

    @Override
    public void doChangeUserPassword(String username, String password, String verify) {
        SimpleAsyncResponseHandler handler = requestCallbackHandler(ModifyPasswordResponseParams.class, username, password);
        mSumbitUserPasswordInfoTaskId = HttpEngineApi.getInstance().modifyPassword(username, password, verify, handler);
    }

    @Override
    public void doQuickRegister(String username, String password) {
        SimpleAsyncResponseHandler handler = requestCallbackHandler(LoginResponseParams.class, username, password);
        mSubmitLoginInfoTaskId = HttpEngineApi.getInstance().register(username, password, handler);
    }

    @Override
    public void doBindOrRealNameVerify(String cellphoneNumber, String verify, String realname, String idCard) {
        SimpleAsyncResponseHandler handler = requestCallbackHandler(BindPhoneResponseParams.class);
        mSumbitVerifyInfoTaskId = HttpEngineApi.getInstance().bindPhoneNumber(cellphoneNumber, verify, realname, idCard, handler);
    }

    @Override
    public void onAsyncRequestException(int flag, Exception ex) {
        super.onAsyncRequestException(flag, ex);
        if (mSubmitLoginInfoTaskId == flag) {
            if (mAutoLoginDialogFragment != null && mAutoLoginDialogFragment.isResumed()) {
                doNextToLogin(mAutoLoginDialogFragment, LoginTypeSwitchWatcher.SWITCH_MODE_WHICH_ACCOUNT);
            }
        }
    }

    @Override
    public void onAsyncRequestResult(int flag, ResponseParams response, Object... params) {
        super.onAsyncRequestResult(flag, response, params);
        if (response != null) {
            int code = response.getState().getCode();
            if (State.SUCCESS == code) {
                if (flag == mSubmitLoginInfoTaskId) {
                    if (response instanceof LoginResponseParams) {
                        parseLoginResponseResult((LoginResponseParams) response, params);
//                    } else if (response instanceof QuickRegisterResponseParams) {
//                        parseOnekeyRegisterResponseResult((QuickRegisterResponseParams) response, params);
                    } else {
                        CallbackObservable observable = CallbackObservable.getInstance();
                        String message = "response type is unknown.";
                        observable.callResponseResult(Constant.CallType.CALL_LOGIN, Constant.LoginState.LOGIN_SUCCESS, message, null);
                    }
                } else if (flag == mSumbitUserPasswordInfoTaskId) {
                    if (response instanceof ModifyPasswordResponseParams) {
                        parseChangeUserPasswordResult((ModifyPasswordResponseParams) response, params);
                    }
//                } else if (flag == mSumbitRegisterInfoTaskId) {
//                    if (response instanceof RegisterResponseParams) {
//                        parseQuickRegisterResponseResult((RegisterResponseParams) response, params);
//                    }
                } else if (flag == mSumbitVerifyInfoTaskId) {
                    if (response instanceof BindPhoneResponseParams) {
                        parseRealNameVerifyResponseResult((BindPhoneResponseParams) response, params);
                    }
                }
            } else {
                //自动登录失败，返回帐号登录界面
                if (response instanceof LoginResponseParams) {
                    if (mAutoLoginDialogFragment != null && mAutoLoginDialogFragment.isResumed()) {
                        doNextToLogin(mAutoLoginDialogFragment, LoginTypeSwitchWatcher.SWITCH_MODE_WHICH_ACCOUNT);
                        return;
                    }
                }

                CallbackObservable observable = CallbackObservable.getInstance();
                String message = response.getState().getMsg();
                observable.callResponseResult(Constant.CallType.CALL_LOGIN, code, message, null);
            }
        }

    }

    private void parseLoginResponseResult(LoginResponseParams response, Object... params) {
        LoginResponseParams.LoginResponseData data = response.getData();

        String uid = data.getUser()==null?"":data.getUser().getId();
        HttpEngineConduits wanSDKDataDao = HttpEngineApi.getInstance().getEngineConduits();
        wanSDKDataDao.writeLastLoginType(mCurrentLoginType);
        wanSDKDataDao.saveCurrentLoginUserUID(uid);

        boolean isAutoLogin = params != null && params.length >= 3 && Boolean.parseBoolean((String) params[2]);

//        if (LoginTypeSwitchWatcher.SWITCH_MODE_WHICH_ACCOUNT == mCurrentLoginType && !isAutoLogin) {
        if (!isAutoLogin) {
            AccountInfo accountInfo = new AccountInfo(uid, data.getUser().getUsername(), params!=null&&params.length>=2?(String)params[1]:data.getToken());
//            dff
//            accountInfo.setEncryptPassword(data.getPassword());
            accountInfo.setEncryptPassword(data.getToken());
            wanSDKDataDao.saveUserAccount(accountInfo);
        }

//        LoginInfo loginInfo = new LoginInfo(uid, data.getToken(), data.getExpires_in());
//        PopupConfig popupConfig = data.getPopupConfig();
//        if(popupConfig!=null&&popupConfig.isIdcardPopup()){
//            //启动定时器服务，提示用户绑定手机/实名认证
//            TimeTaskService.enqueueWork(this, data.getIdcardPopupConfigs());
//        }

//        if (mAccountListDialogFragment != null && mAccountListDialogFragment.isResumed()) {
//            judgeLoginNextOperationSteps(popupConfig, loginInfo, mAccountListDialogFragment);
//        } else
            if (mAutoLoginDialogFragment != null && mAutoLoginDialogFragment.isResumed()) {
            judgeLoginNextOperationSteps(data, mAutoLoginDialogFragment);
        } else {
            judgeLoginNextOperationSteps(data, mLoginFragment);
        }
    }

//    private boolean isGoToBindCellPhone(PopupConfig popupConfig) {
////        boolean bindPhoen =HttpEngineApi.getInstance().getEngineConduits().readIDcardUiType()==0;
////        if (bindPhoen&&popupConfig != null && popupConfig.isPhonePopup()) {
////            return true;
////        }
////
////        if (popupConfig != null && popupConfig.isIdcardPopup()) {
////            return true;
////        }
//
//        return true;
//    }

    private void judgeLoginNextOperationSteps(LoginResponseParams.LoginResponseData data, DialogFragment fragment) {
        HttpEngineConduits wanSDKDataDao = HttpEngineApi.getInstance().getEngineConduits();
        Stack<String> flowStack = wanSDKDataDao.getPlatformConfigInfo().getFlowStack();
        Log.d(TAG,flowStack.size()+"...flowStack");
        if(flowStack!=null&&!flowStack.isEmpty()){
            String flow = flowStack.pop();
            if("identify".equals(flow)){
//                judgeLoginNextOperationSteps(data,fragment);
                boolean needIdentify = wanSDKDataDao.readNeedIdentify();
                if(needIdentify){
                    //跳转到绑定手机号码页面
                    Bundle bundle = new Bundle();
                    bundle.putBoolean(Bundle_IsBindPhone,false);
                    bundle.putSerializable(Bundle_Login,data);
                    doNextToRealNameVerify(fragment, bundle);
                }else{
                    judgeLoginNextOperationSteps(data,fragment);
                }
            }else if ("notice".equals(flow)){
                ArrayList<GameNoticeModel> list = data.getNotice();
                if(list!=null&&list.size()>0&&wanSDKDataDao.readNeedNotice()){
                    Bundle bundle = new Bundle();
                    bundle.putSerializable(Bundle_Notice,list);
                    bundle.putSerializable(Bundle_Login,data);
                    doNextToGameNotice(fragment,bundle);
                }else {
                    judgeLoginNextOperationSteps(data,fragment);
                }
            }else if ("phone".equals(flow)){
                if(1!=data.getUser().getPhoneStatus()){
                    TimeTaskService.enqueueWork(this,wanSDKDataDao.getPlatformConfigInfo().getPhonePopupTime());
                }
                judgeLoginNextOperationSteps(data,fragment);
            }
        }else{
            WL.w(TAG, "login finished.");
            String message = getString(getStringIdentifier("hnssn_sdk_messag_login_successs"));
            LoginInfo loginInfo = new LoginInfo(data.getUser().getId(), data.getToken(), data.getExpires_in());
            doCallbackLoginResult(Constant.LoginState.LOGIN_SUCCESS, message, loginInfo);
        }

//        Popup infoPopup = popupConfig != null ? popupConfig.getNoticePopup() : null;
//        ArrayList<GameNoticeModel> noticeModels = popupConfig==null?null:popupConfig.getGameNoticeList();
//        if(noticeModels!=null&&noticeModels.size()>0){
//            Bundle bundle = new Bundle();
//            bundle.putSerializable("notice",noticeModels);
//            bundle.putParcelable("loginInfo", loginInfo);
//            bundle.putParcelable("popupConfig", popupConfig);
//            doNextToGameNotice(fragment,bundle);
//        }else if (isGoToBindCellPhone(popupConfig)) {
//            //跳转到绑定手机号码页面
//            Bundle bundle = new Bundle();
//            bundle.putParcelable("loginInfo", loginInfo);
//            bundle.putParcelable("PopupConfig", popupConfig);
//            if (popupConfig!=null&&popupConfig.isNoticePopup() && infoPopup != null) {
//                bundle.putParcelable("infoPopup", infoPopup);
//            }
//
//            doNextToRealNameVerify(fragment, bundle);
//        } else if (popupConfig != null && popupConfig.isNoticePopup() && infoPopup != null) {
//            Bundle bundle = new Bundle();
//            bundle.putParcelable("loginInfo", loginInfo);
//            bundle.putParcelable("infoPopup", infoPopup);
//
//            doNextToNoticeTip(fragment, bundle);
//        } else {
//            WL.w(TAG, "login finished.");
//            String message = getString(getStringIdentifier("hnssn_sdk_messag_login_successs"));
//            doCallbackLoginResult(Constant.LoginState.LOGIN_SUCCESS, message, loginInfo);
//        }
    }

    private void judgeBindingNextOperationSteps(BaseDialogFragment fragment, boolean isCanceled) {
        if (isRealNameVerifyBeforePayment()) {
            if (!isCanceled) {
                //跳转到支付界面
                Bundle bundle = fragment.getArguments();
                Intent intent = new Intent(Constant.LocalIntentAction.INTENT_AACTION_START_PAY_ACTIVITY);
                intent.setPackage(getApplicationContext().getPackageName());
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                intent.putExtra("goods", (Goods)bundle.getParcelable("goods"));
                getApplicationContext().startActivity(intent);
            }
            finish();
        } else if (!isOnlyRealNameVerify()) {
            Bundle bundle = fragment.getArguments();
            LoginResponseParams.LoginResponseData data = (LoginResponseParams.LoginResponseData)bundle.getSerializable(Bundle_Login);
            judgeLoginNextOperationSteps(data,fragment);
//            Popup infoPopup = bundle.getParcelable("infoPopup");
//            if (infoPopup != null) {
//                doNextToNoticeTip(mRealNameVerifyFragment, bundle);
//            } else {
//                LoginInfo loginInfo = bundle.getParcelable("loginInfo");
//                String message = getString(getStringIdentifier("hnssn_sdk_messag_login_successs"));
//                doCallbackLoginResult(Constant.LoginState.LOGIN_SUCCESS, message, loginInfo);
//            }
        } else {
            finish();
        }
    }



//    private void parseOnekeyRegisterResponseResult(QuickRegisterResponseParams response, Object... params) {
//        QuickRegisterResponseParams.QuickRegisterResponseData data = response.getData();
//
//        HttpEngineConduits wanSDKDataDao = HttpEngineApi.getInstance().getEngineConduits();
//        wanSDKDataDao.saveCurrentLoginUserUID(data.getId());
//        AccountInfo accountInfo = new AccountInfo(data.getId(), data.getName(), data.getPassword());
//        accountInfo.setEncryptPassword(data.getPassword());
//        wanSDKDataDao.saveUserAccount(accountInfo);
//        Bundle bundle = new Bundle();
//        LoginInfo loginInfo = new LoginInfo(data.getId(), data.getToken(), data.getExpires_in());
//        bundle.putParcelable("loginInfo", loginInfo);
//        bundle.putSerializable("quickRegister", data);
//        doNextToOneKeyLogin(bundle);
//    }

//    private void parseQuickRegisterResponseResult(RegisterResponseParams response, Object... params) {
//        RegisterResponseParams.RegisterResponseData data = response.getData();
//
//        String uid = data.getId();
//        HttpEngineConduits wanSDKDataDao = HttpEngineApi.getInstance().getEngineConduits();
//        wanSDKDataDao.saveCurrentLoginUserUID(uid);
//
//        AccountInfo accountInfo = new AccountInfo(uid, (String) params[0], (String) params[1]);
//        accountInfo.setEncryptPassword(data.getToken());
//        wanSDKDataDao.saveUserAccount(accountInfo);
//
//        //跳转到绑定手机号码页面
//        LoginInfo loginInfo = new LoginInfo(uid, data.getToken(), data.getExpires_in());
//        PopupConfig popupConfig = data.getPopupConfig();
//        judgeLoginNextOperationSteps(popupConfig, loginInfo, mQuickRegisterFragment);
//        if(popupConfig.isIdcardPopup()){
//            //启动定时器服务，提示用户绑定手机/实名认证
//            TimeTaskService.enqueueWork(this, data.getIdcardPopupConfigs());
//        }
//    }

    /**
     * change user password result parse
     *
     * @param response
     * @param params
     */
    private void parseChangeUserPasswordResult(ModifyPasswordResponseParams response, Object...
            params) {
        if (State.SUCCESS == response.getState().getCode()) {
            HttpEngineConduits engineConduits = HttpEngineApi.getInstance().getEngineConduits();
            AccountInfo accountInfo = engineConduits.findUserAccountByUsername((String) params[0]);
            if (accountInfo != null) {
                accountInfo.setPassword((String) params[1]);
            } else {
                accountInfo = new AccountInfo((String) params[0], (String) params[1]);
            }
            engineConduits.saveUserAccount(accountInfo);

            if (mChangePasswordFragment != null && mChangePasswordFragment.isResumed()) {
                mChangePasswordFragment.doCallbackSubmitReuslt();
            }

        }
    }

    private void parseRealNameVerifyResponseResult(BindPhoneResponseParams response, Object... params) {
        if(response.getData().getResult()==1){
            judgeBindingNextOperationSteps(mRealNameVerifyFragment, false);
        }else{
            Toast.makeText(this,"实名认证失败",Toast.LENGTH_SHORT).show();
        }
//        if (mRealNameVerifyFragment.isNextRealNameVerify()) {
//            mRealNameVerifyFragment.setCurrentItem(1);
//        } else {
//            judgeBindingNextOperationSteps(mRealNameVerifyFragment, false);
//        }
    }

    private void doCallbackLoginResult(int stateCode, String message, LoginInfo loginInfo) {
        String repsonse = loginInfo == null ? null : loginInfo.toString();
        CallbackObservable observable = CallbackObservable.getInstance();
        observable.callResponseResult(Constant.CallType.CALL_LOGIN, stateCode, message, repsonse);
        finish();
    }
}
